package com.hzz.dao.eybase;

import com.hzz.framework.dao.IBaseDao;
import com.hzz.framework.entity.IEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;

/**
 * <p>ey_河长 数据库处理接口类。
 */
@Mapper
public interface IEyBaseRetrieveGmDao extends IBaseDao {

    /**
     * 判断是否存在
     */
    @Override
    @Select("select IFNULL(count(0),0) as count from ey_base_retrieve_gm where  id = #{id} ")
    int isDataExist(IEntity entity) throws Exception;

    /**
     * 根据主键 物理删除
     */
    @Override
    @Delete("delete from ey_base_retrieve_gm where  id = #{id} ")
    int deleteByPrimaryKey(IEntity entity) throws Exception;

    int dataStatistics(IEntity entity) throws Exception;

    @Delete("delete from ey_base_retrieve_gm where IFNULL(datediff(DATE_FORMAT(now(),'%Y%m%d'),DATE_FORMAT(date_created,'%Y%m%d')),999)>#{expireDay} ")
    int deleteByExpireDay(@Param("expireDay") Integer expireDay) throws Exception;

    @Select("SELECT " +
            " u.account AS 'user.account'," +
            " u.del_flag AS 'user.del'," +
            " u.id AS 'user.id'," +
            " u.name AS 'user.name'," +
            " gm.id AS 'gm.id'," +
            " gm.name AS 'gm.name'," +
            " (SELECT count( 1 ) FROM ey_base_retrieve_gm WHERE u.account = ey_base_retrieve_gm.phone) AS 'gm.count'," +
            " (SELECT GROUP_CONCAT( ey_base_retrieve_gm.id ) FROM ey_base_retrieve_gm WHERE u.account = ey_base_retrieve_gm.phone) AS 'gm.mIds'" +
            " FROM ey_base_retrieve_gm gm " +
            " INNER JOIN `user` u ON gm.phone = u.account and u.del_flag=0" +
            " WHERE u.id != gm.id" +
            " AND gm.pac LIKE '511025%' ")
    List<HashMap<String, Object>> findGmAbnormalAccount() throws Exception;
}
